import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import login from "../pages/login/login.vue"
// import index from "../pages/index/index.vue"
// import detail from "../pages/detail/detail.vue"
// import search from "../pages/search/search.vue"
// import list from "../pages/list/list.vue"
// import register from "../pages/register/register.vue"
// import test from "../pages/test/test.vue"

// import home from "../pages/home/home.vue"
// import cate from "../pages/cate/cate.vue"
// import shop from "../pages/shop/shop.vue"
let index = () =>
    import ("../pages/index/index.vue")
let detail = () =>
    import ("../pages/detail/detail.vue")
let search = () =>
    import ("../pages/search/search.vue")
let list = () =>
    import ("../pages/list/list.vue")
let register = () =>
    import ("../pages/register/register.vue")
let test = () =>
    import ("../pages/test/test.vue")

let home = () =>
    import ("../pages/home/home.vue")
let cate = () =>
    import ("../pages/cate/cate.vue")
let shop = () =>
    import ("../pages/shop/shop.vue")

// import() 返回值是一个promise对象
let mine = () => {
    return Promise.resolve(
        import ("../pages/mine/mine.vue"))
}


//路由规则
const routes = [{
        path: "/login",
        component: login,
        name: "登录",
        //元信息
        meta: {
            title: "手机登录"
        }
    },
    {
        path: "/register",
        name: "注册",
        meta: {
            title: "注册"
        },
        // 配置多个组件
        components: {
            default: register,
        }
    },
    {
        path: "/index",
        component: index,
        //index的二级路由规则
        children: [{
                path: "home",
                component: home,
                meta: {
                    title: "小U商城"
                },
            },
            {
                path: "cate",
                component: cate,
                meta: {
                    title: "分类"
                }
            },
            {
                path: "shop",
                component: () =>
                    import ("../pages/shop/shop.vue"),
                meta: {
                    title: "购物车"
                }
            },
            {
                path: "mine",
                component: mine,
                meta: {
                    title: "个人中心"
                }
            },
            //二级重定向
            {
                path: "",
                redirect: "home"
            }
        ]
    },
    {
        path: "/search",
        component: search,
        name: "搜索",
        meta: {
            title: "搜索"
        },
        //别名
        alias: "/s"
    },
    {
        path: "/list",
        component: list,
        meta: {
            title: "分类列表",
            keep: true
        }

    },
    {
        path: "/detail",
        component: detail,
        meta: {
            title: '商品详情'
        },
        // beforeEnter(to, from, next) {
        //     console.log("路由独享守卫：beforeEnter");
        //     let type = localStorage.getItem("type")

        //     //打野才能看
        //     if (type === "2") {
        //         next()
        //     } else {
        //         next("/index/mine")
        //     }
        // }
    },
    {
        path: "/order",
        component: () =>
            import ("../pages/order/order.vue"),
        meta: {
            title: "确认订单"
        }
    },
    {
        path: "/addressList",
        component: () =>
            import ("../pages/addressList/addressList.vue"),
        meta: {
            title: "地址列表"
        }
    },
    {
        path: "/addressAdd",
        component: () =>
            import ("../pages/addressAdd/addressAdd.vue"),
        meta: {
            title: "地址编辑"
        }
    },
    {
        path: "*",
        redirect: "/login"
    }
]

const router = new VueRouter({
    mode: "hash",
    routes: routes,

    //滚动行为
    scrollBehavior(to, from, savePosition) {
        /*如果有历史记录，savePosition保留的是历史记录中的滚动位置；
        如果没有历史记录，savePostion是null .
        我们通过判断savePostion是否存在，存在就返回savePostion的位置；
        不存在，就返回（0,0，）*/
        if (savePosition) {
            return savePosition
        } else {
            return {
                x: 0,
                y: 0
            }
        }
    }
})

router.beforeEach((to, from, next) => {
    //先判断是不是登录或者注册的路由，直接 next
    if (to.path === "/login" || to.path == "/register") {
        next()
        return;
    }
    //如果不是登录和注册，判断是否登录，登录了next()
    let isLogin = localStorage.getItem("isLogin");
    if (isLogin) {
        next()
        return;
    }
    //没有登录
    next("/login")
})
router.afterEach((to, from) => {
    console.log("进入完成 afterEach");
})

export default router